﻿var DataSource = function(){
	this.dataSource = [];
	this.typeCheck = YAHOO.lang;
}
DataSource.prototype = {
	/**
	 * 添加数据 
	 * @type public method
	 * @param {array} data data 参数为json对象数组
	 * @return {array} json data 返回添加后的当前数据
	 */
	add:function(data){
		//是否是数组类型
		if(this.typeCheck.isArray(data)){
			if(data.length == 0 ) return false;
			if(data.length == 1 ) return this._addJsonData(data[0]);
			if(data.length > 1 ) return this._addArrayData(data);
		//是否是对象类型
		}else if(this.typeCheck.isObject(data)){
			return this._addJsonData(data);
		}	
	},
	/**
	 * 添加一条，参数为json对象类型
	 * @type private method
	 * @param {json object} data data参数为json对象类型
	 */
	_addJsonData:function(data){
		//this.printJson(data);
		if(data){
			this.dataSource.push(data);
		}
	},
	
	/**
	 * 添加多条数据，参数为数组类型
	 * @type private method
	 * @param {array} dataArr dataArr参数为json对象数组
	 */
	_addArrayData:function(dataArr){
		var length;
		//contact
		if(dataArr){
			length = dataArr.length;
			if(length > 1){
				for(var i=0;i<length;i++){
					this._addJsonData(dataArr[i]);
				}
			}			
		}
	},
	/**
	 * 删除数据 
	 * @type public method
	 * @param {array} data data参数为json对象数组
	 */
	del:function(data){
		if(this.typeCheck.isArray(data)){
			for(var i=0,length=data.length;i<length;i++){
				this._delData(data[i]);
			}
		}else{				
			this._delData(data);		
		}	
	},
	/**
	 * 删除符合data条件的数据 
	 * @type private method
	 * @param {data} data data参数为json对象类型
	 */
	_delData:function(data){
		var index,resData;		
		if(this.typeCheck.isNumber(data)){
			this.dataSource.splice(data,1);
			return;
		}		
		//搜索数据
		resData = this.search(data);		
		//如果搜索结果为数组对象
		if(this.typeCheck.isArray(resData)){
			for(var i = resData.length - 1; i >= 0; i--){
				index = resData[i].index;
				if(this.dataSource[index]){
					this.dataSource.splice(index,1);				
				}	
			}
		//搜索结果为对象类型
		}else if(this.typeCheck.isObject(resData)){
			index = resData.index;
			if(this.dataSource[index]){
				this.dataSource.splice(index,1);				
			}
		}
	},
	/**
	 * 更新数据,把参数1中的数据更新为参数2
	 * @type public method
	 * @param {array} oldData 参数为json对象数组/json数据
	 * @param {array} newData 参数为json对象
	 * @return {array} json data 返回更新后的当前数据
	 */
	update:function(oldData,newData){
		var index,resData;
		//如果第一个参数是整型
		if(this.typeCheck.isNumber(resData)){
			index = oldData;
			this._updateData(this.dataSource[index],newData);
			return;
		}
		//搜索数据
		resData = this.search(oldData);
		//如果搜索结果为数组对象
		if(this.typeCheck.isArray(resData)){
			for(var i = 0,length = resData.length; i < length; i++){
				index = resData[i].index;
				if(this.dataSource[index]){
					this._updateData(this.dataSource[index],newData);
				}
			}
		//搜索结果为对象类型
		}else if(this.typeCheck.isObject(resData)){
			index = resData.index;
			if(this.dataSource[index]){
				this._updateData(this.dataSource[index],newData);
			}
		}
	},
	_updateData:function(oldData,newData){
		if(!oldData) return;
		if(newData){
			for(var newKey in newData){
				for(var oldKey in oldData){
					if(newData[newKey]){
						if(newKey == oldKey){
							oldData[oldKey] = newData[newKey];
						}else{
							oldData[newKey] = newData[newKey];
						}
					}
				}
			}
		}
	},
	/**
	 * 搜索数据
	 * @type public method
	 * @param {object} data data 参数为json对象数组 或 json 数据对象
	 * @return {array} json data 返回json数组
	 */
	search:function(data){
		//是否是数组类型
		if(this.typeCheck.isArray(data)){			
			if(data.length == 0 ) return false;
			if(data.length == 1 ) return this._searchByJsonData(data[0]);
			if(data.length > 1 ) return this._searchByArrayData(data);
		//是否是对象类型
		}else if(this.typeCheck.isObject(data)){			
			return this._searchByJsonData(data);
		}		
	},	

	/**
	 * 搜索单条数据根据json对象
	 * @type public method
	 * @param {object} data data 参数为json 数据对象
	 * @return {array} json data 返回json格式数据
	 */
	_searchByJsonData:function(data){
		var ds = this.dataSource,isEqual,resData = [];
		var length;
		if(ds.length && ds.length > 0){			
			length = ds.length;
			for(var i=0;i<length;i++){
				//如果2个对象不相等
				if(data != ds[i]){
					isEqual = true;
					//扫描一条数据属性是否相等
					for(var key in data){						
						isEqual = isEqual && ((data[key] == ds[i][key]) ? true : false);
					}
					//如果符合条件返回
					if(isEqual){					
						resData.push({
							index:i,
							data:ds[i]
						});
					}
				//相等直接返回
				}else{				
					resData.push({
						index:i,
						data:ds[i]
					});
				}
			}			
		}
		return (resData.length > 0) ? ((resData.length == 1) ? resData[0] : resData) : false;
	},	

	/**
	 * 搜索多条数据根据json对象数组
	 * @type public method
	 * @param {object} dataArr 参数为json 数据对象数组
	 * @return {array} json data 返回json 数据数组
	 */
	_searchByArrayData:function(dataArr){
		var resultData = [],resultDataSearch;
		if(dataArr){
			for(var i=0,length = dataArr.length;i<length;i++){
				resultDataSearch = this._searchByJsonData( dataArr[i] );	
				if(resultDataSearch){
					if(this.typeCheck.isArray(resultDataSearch)){
						resultData = this._pushArr(resultData,resultDataSearch);						
					}else if(this.typeCheck.isObject(resultDataSearch)){
						resultData.push(resultDataSearch);
					}
				}
			}
		}
		return (resultData.length == 0) ? false : resultData;
	},
	_pushArr:function(toArr,arr){
		if(!toArr)return;
		if(arr){
			for(var i=0,length = arr.length; i<length; i++){
				toArr.push(arr[i]);
			}
		}
		return toArr;
	},
	renderDataVeiw:function(container){
		var ds = this.dataSource;
		var tableStr,theadStr,tbodyStr,trStr;
		tableStr = '<table>';
		theadStr = '<thead><tr>';
		for(var thKey in ds[0]){
			theadStr += '<th>' + thKey + '</th>';
		}
		theadStr += '</tr></thead>';
		tableStr += theadStr;
		tbodyStr = '<tbody>';
		for(var i = 0; i < ds.length; i++){
			trStr = '<tr>';
			for(var tdKey in ds[i]){
				trStr += '<td>' + ds[i][tdKey] + '</td>';
			}
			trStr += '</tr>';
			tbodyStr += trStr;
		}
		tableStr += tbodyStr;
		tableStr += '</table>';
		get(container).innerHTML = tableStr;
	}
}

/*
//数据行为
var DataBehavior = function(){
	printData:function(){
		var ds = this.dataSource;
		var str = '', subStr = '';
		for(var i=0,length = ds.length;i<length;i++){
			subStr = ''
			for(var key in ds[i]){
				subStr += key + ":" + ds[i][key] + ",";
			}
			subStr += '\n';
			str += subStr;
		}		
	},
	printJson:function( jsonData ){
		alert(Joo.JSON.stringify( jsonData ));
	},
	renderDataVeiw:function(container){
		var ds = this.dataSource;
		var tableStr,theadStr,tbodyStr,trStr;
		tableStr = '<table>';
		theadStr = '<thead><tr>';
		for(var thKey in ds[0]){
			theadStr += '<th>' + thKey + '</th>';
		}
		theadStr += '</tr></thead>';
		tableStr += theadStr;
		tbodyStr = '<tbody>';
		for(var i = 0; i < ds.length; i++){
			trStr = '<tr>';
			for(var tdKey in ds[i]){
				trStr += '<td>' + ds[i][tdKey] + '</td>';
			}
			trStr += '</tr>';
			tbodyStr += trStr;
		}
		tableStr += tbodyStr;
		tableStr += '</table>';
		get(container).innerHTML = tableStr;
	}
}
*/